﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FaultDiagnosisSystem.BLL
{
    public class TenMeanData
    {
        DAL.TenMeanData dal = new DAL.TenMeanData();
        /// <summary>
        /// 指定日期和风机,获取所有的传感器数据视图集合
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.DataModels.v_TenMeanData> GetViewList(DateTime day, int turbineId)
        {
            return dal.GetViewList(day, turbineId);
        }

        /// <summary>
        /// 指定风机,获取最近一天所有的传感器数据视图集合
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.DataModels.v_TenMeanData> GetViewListOfLastDay(int turbineId)
        {
            return dal.GetViewListOfLastDay(turbineId);
        }

        /// <summary>
        /// 指定日期,获取所有的传感器数据视图集合，按时间升序排列
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.DataModels.v_TenMeanData> GetViewList(DateTime day)
        {
            return dal.GetViewList(day);
        }

        /// <summary>
        /// 指定时间,获取该时间之后的所有的传感器数据视图集合，按时间升序排列
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.DataModels.v_TenMeanData> GetViewList2(DateTime timeStart)
        {
            return dal.GetViewList2(timeStart);
        }

        /// <summary>
        /// 指定日期和风机,获取主传动链下的传感器业务视图集合
        /// </summary>
        /// <param name="day"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.ViewModels.Com5_SensorDataViewModel> GetCom5_ViewList(DateTime day, int turbineId)
        {
            return GetViewList(day,turbineId)
                .Select(e=>new Models.ViewModels.Com5_SensorDataViewModel() {
                    TenMeanId=e.TenMeanId,
                    TimeStamp=e.TimeStamp,
                    TurbineId=e.TurbineId,
                    TurbineName=e.TurbineName,
                    TenMeanFarmDataId=e.TenMeanFarmDataId,
                    GBoxDegC_FarmMean=e.GBoxDegC_FarmMean,
                    GBoxDegC_Mean=e.GBoxDegC_Mean,
                    GenDegC_FarmMean=e.GenDegC_FarmMean,
                    GenDegC_Mean=e.GenDegC_Mean
                }).ToList();    
        }

        /// <summary>
        /// 指定风机,获取最近一天主传动链下的传感器业务视图集合
        /// </summary>
        /// <param name="day"></param>
        /// <param name="turbineId"></param>
        /// <returns></returns>
        internal IList<Models.ViewModels.Com5_SensorDataViewModel> GetCom5_ViewListOfLastDay(int turbineId)
        {
            return GetViewListOfLastDay(turbineId)
                .Select(e => new Models.ViewModels.Com5_SensorDataViewModel()
                {
                    TenMeanId = e.TenMeanId,
                    TimeStamp = e.TimeStamp,
                    TurbineId = e.TurbineId,
                    TurbineName = e.TurbineName,
                    TenMeanFarmDataId = e.TenMeanFarmDataId,
                    GBoxDegC_FarmMean = e.GBoxDegC_FarmMean,
                    GBoxDegC_Mean = e.GBoxDegC_Mean,
                    GenDegC_FarmMean = e.GenDegC_FarmMean,
                    GenDegC_Mean = e.GenDegC_Mean
                }).ToList();
        }

        /// <summary>
        /// 指定日期，构建风机数据字典
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        internal Dictionary<string,List<Models.DataModels.v_TenMeanData>> GetTurbineViewDict(DateTime day)
        {
            /*
              字典格式
              {
                turbine1：tenMeanList，
                turbine2：tenMeanList
                ...
              }
            */
            var dict = new Dictionary<string, List<Models.DataModels.v_TenMeanData>>();

            //插入数据
            var list = GetViewList(day);
            foreach (var tenMean in list)
            {
                if (!dict.ContainsKey(tenMean.TurbineId.ToString()))
                {
                    dict.Add(tenMean.TurbineId.ToString(), new List<Models.DataModels.v_TenMeanData>());
                }
                dict[tenMean.TurbineId.ToString()].Add(tenMean);
            }

            return dict;

        }

        /// <summary>
        /// 指定起始时间点，根据之后的所有数据，构建风机数据字典
        /// </summary>
        /// <param name="timeStart"></param>
        /// <returns></returns>
        internal Dictionary<string, List<Models.DataModels.v_TenMeanData>> GetTurbineViewDict2(DateTime timeStart)
        {
            /*
              字典格式
              {
                turbine1：tenMeanList，
                turbine2：tenMeanList
                ...
              }
            */
            var dict = new Dictionary<string, List<Models.DataModels.v_TenMeanData>>();

            //插入数据
            var list = GetViewList2(timeStart);
            foreach (var tenMean in list)
            {
                if (!dict.ContainsKey(tenMean.TurbineId.ToString()))
                {
                    dict.Add(tenMean.TurbineId.ToString(), new List<Models.DataModels.v_TenMeanData>());
                }
                dict[tenMean.TurbineId.ToString()].Add(tenMean);
            }

            return dict;

        }
    }
}